package com.sfac.springboot.modules.test.controller;

import com.sfac.springboot.modules.common.vo.Result;
import com.sfac.springboot.modules.common.vo.Search;
import com.sfac.springboot.modules.test.entity.Student;

import com.sfac.springboot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;
    /**
     * 127.0.0.1/api/student ---- post
     * {"studentName":"hhh"}
     * {"studentName":"hhh", "studentCard":{"cardNum":"202250111"}}
     */
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student ---- put
     * {"id":1,"studentName":"hh"}
     */
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/student/11 ---- delete
     */
    @DeleteMapping(value = "/student/{id}")
    public Result<Object> deleteStudentById(@PathVariable int id) {
        return studentService.deleteStudentById(id);
    }

    /**
     * 127.0.0.1/api/student/2 ---- get
     */
    @GetMapping(value = "/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }


    /**
     * 127.0.0.1/api/students----post
     * {"currentPage":1,"pageSize":5,"sort":"studentName","direction":"desc","keyword":""} //参数可变
     */
    @PostMapping(value = "students",consumes = "application/json")
    public Page getStudentBySearch(@RequestBody Search search) {
        return studentService.getStudentBySearch(search);
    }


    /**
     *127.0.0.1/api/students?studentName=  ----get
     */
    @GetMapping("/students")
    public List<Student> getStudentsByStudentName(@RequestParam String studentName) {
        return studentService.getStudentsByStudentName(studentName);
    }
}
